﻿    //新建XMLHttpRequest对象实例
    var xmlHttp = false;
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      try
      {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) 
      {
        try 
        {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) 
        {
          xmlHttp = false;
        }
      }
    }
    if (!xmlHttp)
    {
       alert("Error initializing XMLHttpRequest!");
    }
    
    //执行请求,txtID为要验证的输入控件ID
    function startRequest(txtId)
    {
        var MyUN=document.getElementById(txtId).value;
        if(MyUN=="")
        {
            document.getElementById("loginName").innerHTML="用户名不能为空!";
            return;
        }
        xmlHttp.open("GET","register.aspx?userName="+MyUN+"&random="+new Date().getTime(),true);
        //回调函数  
        xmlHttp.onreadystatechange = checkUserCallBack;
        xmlHttp.send(null);
    }
    //回调函数
    function checkUserCallBack()
    {
        if(xmlHttp.readyState==4)
        {
            if(xmlHttp.status==200)
            {
                var xmlDoc1=xmlHttp.responseXML;
                var xmlDoc=xmlHttp.responseXML.documentElement;
                var checkResult=xmlDoc.getElementsByTagName("result")[0].childNodes[0].nodeValue;
                if(checkResult=="false")
                {
                    //loginName为要显示提示信息的控件
                    document.getElementById("loginName").style.visibility="visible";
                    document.getElementById("loginName").innerHTML="用户名存在!";
                }
                else
                {
                    document.getElementById("loginName").style.visibility="hidden";
                }
            }
        }
    }